Python CSV Module
π CSV Shenanigans in Python: A Fun-Filled Dive Into Dataβ
The CSV (Comma Separated Values) format is like the fast food of data storageβsimple, cheap, and almost everywhere! Used by spreadsheets, databases, and your nosy co-worker who insists on using Excel for everything, CSVs are a go-to format.
Python, our trusty data-slinging sidekick, comes equipped with the csv
module to make our CSV-crunching adventures a breeze.
In this magical rollercoaster, weβll explore:
csv.reader()
(the eavesdropper of CSV rows)csv.writer()
(the gossip writer)csv.DictReader()
(the nosy neighbor who knows all the names)csv.DictWriter()
(the formal correspondent)
We'll also jazz things up with custom delimiters and dialects. So, grab a cup of coffee (or tea or Red Bull), and letβs CSV like pros!
π©βπ©βπ§βπ¦ Our Guinea Pig: person.csvβ
Behold! A humble CSV containing details of four fabulous humans:
Name,Age,City,Occupation,Email
Alice,29,New York,Engineer,alice@example.com
Bob,35,Los Angeles,Doctor,bob@example.com
Charlie,45,Chicago,Teacher,charlie@example.com
Diana,32,Miami,Artist,diana@example.com
π§ 1. Reading a CSV Fileβ
1.1. Using csv.reader()
β The βJust Read It!β Methodβ
Letβs read the file row by row like a digital archaeologist uncovering ancient secrets (but with less sand).
import csv
file_path = 'person.csv'
with open(file_path, mode='r', newline='', encoding='utf-8') as file:
reader = csv.reader(file)
for row in reader:
print(row)
Output:
['Name', 'Age', 'City', 'Occupation', 'Email']
['Alice', '29', 'New York', 'Engineer', 'alice@example.com']
['Bob', '35', 'Los Angeles', 'Doctor', 'bob@example.com']
['Charlie', '45', 'Chicago', 'Teacher', 'charlie@example.com']
['Diana', '32', 'Miami', 'Artist', 'diana@example.com']
1.2. Using csv.DictReader()
β For Those Who Love Labelsβ
Why settle for anonymous lists when you can have glorious dictionaries?
import csv
file_path = 'person.csv'
data_dict_read = []
with open(file_path, mode='r', newline='', encoding='utf-8') as file:
reader = csv.DictReader(file)
for row in reader:
data_dict_read.append(row)
print(row)
Output (now with 100% more keys!):
{'Name': 'Alice', 'Age': '29', 'City': 'New York', 'Occupation': 'Engineer', 'Email': 'alice@example.com'}
{'Name': 'Bob', 'Age': '35', 'City': 'Los Angeles', 'Occupation': 'Doctor', 'Email': 'bob@example.com'}
{'Name': 'Charlie', 'Age': '45', 'City': 'Chicago', 'Occupation': 'Teacher', 'Email': 'charlie@example.com'}
{'Name': 'Diana', 'Age': '32', 'City': 'Miami', 'Occupation': 'Artist', 'Email': 'diana@example.com'}
βοΈ 2. Writing a CSV Fileβ
2.1. Using csv.writer()
β CSV Matchmakerβ
Letβs write data like weβre sending a letter to a pen pal who only speaks Excel.
import csv
data = [
['Name', 'Age', 'City', 'Occupation', 'Email'],
['Alice', 29, 'New York', 'Engineer', 'alice@example.com'],
['Bob', 35, 'Los Angeles', 'Doctor', 'bob@example.com'],
['Charlie', 45, 'Chicago', 'Teacher', 'charlie@example.com'],
['Diana', 32, 'Miami', 'Artist', 'diana@example.com']
]
file_path = 'person_new.csv'
with open(file_path, mode='w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerows(data)
Creates: person_new.csv
2.2. Using csv.DictWriter()
β A Letter from One Dict to Anotherβ
Feeling fancy? Write your dictionaries directly and keep those column headers intact!
import csv
data_dicts = [
{'Name': 'Alice', 'Age': 29, 'City': 'New York', 'Occupation': 'Engineer', 'Email': 'alice@example.com'},
{'Name': 'Bob', 'Age': 35, 'City': 'Los Angeles', 'Occupation': 'Doctor', 'Email': 'bob@example.com'},
{'Name': 'Charlie', 'Age': 45, 'City': 'Chicago', 'Occupation': 'Teacher', 'Email': 'charlie@example.com'},
{'Name': 'Diana', 'Age': 32, 'City': 'Miami', 'Occupation': 'Artist', 'Email': 'diana@example.com'}
]
file_path_dict_writer = 'person_dict_written.csv'
with open(file_path_dict_writer, mode='w', newline='', encoding='utf-8') as file:
fieldnames = ['Name', 'Age', 'City', 'Occupation', 'Email']
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
for row in data_dicts:
writer.writerow(row)
π§ 3. Custom Delimiters and Quote Charactersβ
Some CSVs are extra. Like using |
for quotes and \t
for tabs.
Hereβs one:
|Name| |Age| |City| |Occupation| |Email|
|Alice| |29| |New York| |Engineer| |alice@example.com|
Read it like a boss:
import csv
with open('example.tsv', mode='r') as file:
reader = csv.reader(file, delimiter='\t', quotechar='|')
for row in reader:
print(row)
π― Quoting Modesβ
Four flavors of quoting:
QUOTE_ALL
: Quote everything! (very dramatic)QUOTE_MINIMAL
: Only quote the spicy bits (default)QUOTE_NONNUMERIC
: Keep numbers nakedQUOTE_NONE
: Raw and unfiltered
import csv
f = open('person_data_new.tsv', 'w')
with f:
writer = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC)
writer.writerows((["coins", 3], ["pens", 2], ["bottles", 7]))
Output:
"coins",3
"pens",2
"bottles",7
π 4. CSV Dialects β Different Accents for the Same Languageβ
Letβs list CSV dialects like a linguist at a data convention:
import csv
names = csv.list_dialects()
for name in names:
print(name)
dialect = csv.get_dialect(name)
print(repr(dialect.delimiter), end=" ")
print(dialect.doublequote, end=" ")
print(dialect.escapechar, end=" ")
print(repr(dialect.lineterminator), end=" ")
print(dialect.quotechar, end=" ")
print(dialect.quoting, end=" ")
print(dialect.skipinitialspace, end=" ")
print(dialect.strict)
Output:
excel
',' True None '\r\n' " 0 False False
excel-tab
'\t' True None '\r\n' " 0 False False
unix
',' True None '\n' " 1 False False
π οΈ 5. Creating Your Own CSV Dialectβ
Because sometimes⦠you just want a #
instead of a ,
.
import csv
csv.register_dialect("hashes", delimiter="#")
f = open('items3.csv', 'w')
with f:
writer = csv.writer(f, dialect="hashes")
writer.writerow(("pencils", 2))
writer.writerow(("plates", 1))
writer.writerow(("books", 4))
Output:
pencils#2
plates#1
books#4
π 6. Conclusionβ
Weβve adventured through the mystical lands of csv.reader()
, DictReader()
, writer()
, DictWriter()
, and even played with weird quote characters and dialects.
CSV may seem like a humble format, but in the right hands (like yours!), itβs a powerful tool.
Now go forth and tame those spreadsheets like the Pythonic warrior you are.
Happy CSV-ing! ππ